package com.rabbitmqConfig;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
	/**
	 * 交换机名称
	 */
	public static final String DIRECT_EXCHANGE="directExchange";
	/**
	 * 队列名称
	 */
	public static final String DIRECT_QUEUE="directQueue";
	/**
	 * 定义 routingKey
	 */
	public static final String routingKey="routingkey";
	@Bean
	public DirectExchange directExchange() {
		return new DirectExchange(DIRECT_EXCHANGE);
	}
	/**
	 * 定义队列
	 * @return
	 */
	@Bean
	public Queue directQueue() {
		return new Queue(DIRECT_QUEUE);
	}
	@Bean
	public Binding directBinding() {
		return BindingBuilder.bind(directQueue())
				.to(directExchange()).with(routingKey);
	}
 
	/*-------------------------Publish/Subscribe-------------------------------*/
	/*---Sending messages to many consumers at once----------------------------*/
	/*-------------------------Publish/Subscribe-------------------------------*/
	/**
	 * fanout 交换机
	 */
	public static final String FANOUT_EXCHANGE="fanoutExchange";
	/**
	 * fanout 队列1
	 */
	public static final String FANOUT_QUEUE1="fanoutqueue1";
	/**
	 * fanout 队列2
	 */
	public static final String FANOUT_QUEUE2="fanoutqueue2";
	@Bean
	public FanoutExchange fanoutexchange() {
		return new FanoutExchange(FANOUT_EXCHANGE);
	}
	@Bean
	public Queue fanoutQueue1() {
		return new Queue(FANOUT_QUEUE1);
	}
	@Bean
	public Queue fanoutQueue2() {
		return new Queue(FANOUT_QUEUE2);
	}
	@Bean
	public Binding fanoutBinding1() {
		return BindingBuilder.bind(fanoutQueue1()).to(fanoutexchange());
	}
	@Bean
	public Binding fanoutBinding2() {
		return BindingBuilder.bind(fanoutQueue2()).to(fanoutexchange());
	}
	
	/**
	 * 路由模式
	 * 任何发送到Direct Exchange的消息都会被转发到RouteKey指定的Queue，这种模式下不需要将Exchange进行任何绑定（binding）操作，
	 * 消息传递时需要一个RouteKey，可以简单的理解为要发送到的队列名字。如果vhost中不存在该队列名，消息会丢失。
	 */
	/**
	 * 交换机名称
	 */
	public static final String DIRECT_ROUTING_EXCHANGE="DIRECT_ROUTING_EXCHANGE";
	/**
	 * routing 队列1
	 */
	public static final String DIRECT_ROUTING_QUEUE1="DIRECT_ROUTING_QUEUE1";
	/**
	 * routing 队列2
	 */
	public static final String DIRECT_ROUTING_QUEUE2="DIRECT_ROUTING_QUEUE2";
	
	/**
	 * 定义队列
	 * @return
	 */
	@Bean
	public Queue directRoutingQueue1() {
		return new Queue(DIRECT_ROUTING_QUEUE1);
	}
	/**
	 * 定义队列
	 * @return
	 */
	@Bean
	public Queue directRoutingQueue2() {
		return new Queue(DIRECT_ROUTING_QUEUE2);
	}
	/**
	 * 定义交换机
	 */
	@Bean 
	public DirectExchange directRoutingExchange() {
		return new DirectExchange(DIRECT_ROUTING_EXCHANGE);
	}
	
	@Bean
	public Binding directRoutingBinding() {
		return BindingBuilder.bind(directRoutingQueue1()).to(directRoutingExchange()).with("error");
	}
	@Bean
	public Binding directRoutingBinding2() {
		return BindingBuilder.bind(directRoutingQueue2()).to(directRoutingExchange()).with("error");
	}
	@Bean
	public Binding directRoutingBinding3() {
		return BindingBuilder.bind(directRoutingQueue2()).to(directRoutingExchange()).with("info");
	}
	@Bean
	public Binding directRoutingBinding4() {
		return BindingBuilder.bind(directRoutingQueue2()).to(directRoutingExchange()).with("warning");
	}
	
	
	/**
	 * topic 模式
	 *主体模式其实就是在路由模式的基础上，支持了对key的通配符匹配（星号以及井号），以满足更加复杂的消息分发场景。
	 *	“#” : 匹配一个或者多个
	 *	“*”：匹配一个
	 *	例如上图中，lazy.#可以匹配到key=lazy.a或者key=lazy.a.b。.orange只能匹配到a.orange,无法匹配a.b.orange
	 *	
	 **/
	/**
	 * 交换机名称
	 */
	public static final String TOPIC_EXCHANGE="TOPIC_EXCHANGE";
	/**
	 * topic 队列1
	 */
	public static final String TOPIC_QUEUE1="TOPIC_QUEUE1";
	/**
	 * topic 队列2
	 */
	public static final String TOPIC_QUEUE2="TOPIC_QUEUE2";
	
	/**
	 * 定义topic 队列
	 */
	@Bean
	public Queue topicQueue1() {
		return new Queue(TOPIC_QUEUE1);
	}
	/**
	 * 定义topic 队列
	 */
	@Bean
	public Queue topicQueue2() {
		return new Queue(TOPIC_QUEUE2);
	}
	/**
	 * 定义topic 交换机
	 */
	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange(TOPIC_EXCHANGE);
	}
	/**
	 * 绑定queue 和交换机
	 * 
	 */
	@Bean
	public Binding topicBinding1() {
		return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.orange.*");
	}
	/**
	 * 绑定queue 和交换机
	 * 
	 */
	@Bean
	public Binding topicBinding2() {
		return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("*.*.rabbit");
	}
	/**
	 * 绑定queue 和交换机
	 * 
	 */
	@Bean
	public Binding topicBinding3() {
		return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lazy.#");
	}
}
